package com.ggh.jinjilive.ui.mine;

import androidx.lifecycle.MutableLiveData;

import com.ggh.jinjilive.mobile.CommonMobile;
import com.ggh.jinjilive.mobile.ResultEntity;
import com.ggh.jinjilive.mobile.mall.MallPointsClass;
import com.ggh.jinjilive.mobile.mine.MembersMobile;
import com.ggh.jinjilive.mobile.mine.TradingDetailMobile;
import com.ggh.jinjilive.remotedata.InterceptObserver;
import com.ggh.jinjilive.remotedata.systemapi.SystemApiService;
import com.ggh.jinjilive.ui.BaseViewModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


//我的公共VM
public class MineViewModel extends BaseViewModel {
    //获取会员列表
    public MutableLiveData<List<MembersMobile>> mMembersMobileList = new MutableLiveData<>();
    //我的收益
    public MutableLiveData<CommonMobile> mEarningsCommonMobile = new MutableLiveData<>();
    //交易明细
    public MutableLiveData<List<TradingDetailMobile>> mTradingDetailMobileList = new MutableLiveData<>();

    public MineViewModel(SystemApiService apiCallInterface) {
        super(apiCallInterface);
    }


    /**
     * 获取会员列表
     */
    public void infoViplist(){
        addSubscribe(apiCallInterface.infoViplist().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<MembersMobile>>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                List<MembersMobile> membersMobileList = (List<MembersMobile>) resultEntity.getData();
                mMembersMobileList.setValue(membersMobileList);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                mMembersMobileList.setValue(null);
            }
        }));
    }

    /**
     * 我的收益
     */
    public void infoEarnings(){
        addSubscribe(apiCallInterface.infoEarnings().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<CommonMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                CommonMobile commonMobile = (CommonMobile) resultEntity.getData();
                mEarningsCommonMobile.setValue(commonMobile);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                mEarningsCommonMobile.setValue(null);
            }
        }));
    }

    /**
     * 积分兑换钻石
     * @param params
     */
    public void infoYbdhzs(Map<String, Object> params){
        addSubscribe(apiCallInterface.infoYbdhzs(params).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<CommonMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                CommonMobile commonMobile = (CommonMobile) resultEntity.getData();
                mEarningsCommonMobile.setValue(commonMobile);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                mEarningsCommonMobile.setValue(null);
            }
        }));
    }

    /**
     * 交易明细
     */
    public void infoLogList(){
        if (isRefresh) {
            page = 1;
        }else {
            page = page + 1;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("from", "1");
        params.put("page",page);
        params.put("limit","20");
        addSubscribe(apiCallInterface.infoLogList(params).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<TradingDetailMobile>>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                List<TradingDetailMobile> tradingDetailMobileList = (List<TradingDetailMobile>) resultEntity.getData();
                mTradingDetailMobileList.setValue(tradingDetailMobileList);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                mTradingDetailMobileList.setValue(null);
            }
        }));
    }

}
